package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.constant.MessageConstant;
import com.xr.mapper.MemberMapper;
import com.xr.mapper.OrderMapper;
import com.xr.mapper.OrderSettingMapper;
import com.xr.pojo.*;
import com.xr.result.Result;
import com.xr.service.OrderService;
import com.xr.service.SetMealService;
import com.xr.utils.DateUtils;
import freemarker.template.utility.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
 * 订单的实现类
 * */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    //预约
    @Transactional
    @Override
    public Result order(Map<String, String> map) {
        System.out.println("mapValue" + map);
//      1、检查用户所选择的预约日期是否已经有预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Integer setmealId = Integer.parseInt(map.get("setmealId"));//准备套餐id
        //ordersetting的辅助类
        OrderSettingExample ose = new OrderSettingExample();
        OrderSettingExample.Criteria osec = ose.createCriteria();
        //日期转换的格式
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = null;
//        try {
//            date = sdf.parse(orderDate);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        //根据预约日期查询出ordersetting
        try {
            osec.andOrderdateEqualTo(DateUtils.parseString2Date(orderDate));
            List<OrderSetting> orderSettings = orderSettingMapper.selectByExample(ose);
            //拿到第一条数据
            OrderSetting orderSetting = orderSettings.get(0);
            //判断ordersetting是否为空
            if (orderSetting == null) {
                //为空 该日期无法进行预约
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
//      2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
            //可预约人数
            Integer number = orderSetting.getNumber();
            //已预约人数
            Integer reservations = orderSetting.getReservations();
            if (reservations >= number) {
                //预约已满
                return new Result(false, MessageConstant.ORDER_FULL);
            }
//      3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
            String telephone = (String) map.get("telephone");
            MemberExample me = new MemberExample();
            MemberExample.Criteria mec = me.createCriteria();
            mec.andPhonenumberEqualTo(telephone);
            List<Member> members = memberMapper.selectByExample(me);
            Member member = new Member();
            System.out.println("membersValue:" + member);
            if (members != null && members.size() > 0) {
                System.out.println("join !null");
                //判断是否在重复预约
                member = members.get(0);
                //预约辅助类
                OrderExample oe = new OrderExample();
                OrderExample.Criteria oec = oe.createCriteria();
                oec.andMemberIdEqualTo(member.getId());
                oec.andOrderdateEqualTo(DateUtils.parseString2Date(orderDate));
                oec.andSetmealIdEqualTo(setmealId);
                Integer id = member.getId();//会员id
                //根据条件查询
                List<Order> orders = orderMapper.selectByExample(oe);
                if (orders.size() > 0 && orders != null) {
                    //用户重复预约
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
                System.out.println("afternoadd" + member);
            } else {
//      4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
                System.out.println("join null");
                member = new Member();
                member.setName(map.get("name"));//姓名
                member.setIdcard(map.get("idCard"));//身份证
                member.setSex(map.get("sex"));//性别
                member.setPhonenumber(telephone);//手机号
                member.setRegtime(new Date());//注册日期
                System.out.println("memberValues:" + member);
                memberMapper.insertSelective(member);
                System.out.println("afteradd" + member);
            }
//      5、预约成功，更新当日的已预约人数
            System.out.println("5Member:" + member);
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderdate(DateUtils.parseString2Date(orderDate));
            order.setSetmealId(setmealId);
            order.setOrderstatus("未到诊");
            orderMapper.insertSelective(order);
            System.out.println("orderV:" + order);

            orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
//            orderSettingMapper.updateByOrderDate(orderSetting);
            orderSettingMapper.updateByPrimaryKeySelective(orderSetting);
            System.out.println("getid:" + order);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ORDERSETTING_FAIL);//预约失败
    }

    //根据ID查询相关info
    @Override
    public Map findById(Integer id) {
        return orderMapper.queryDetailInfo(id);
    }

}
